/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easyanimation.panel;

import easyanimation.frame.CConfig;
import easyanimation.frame.Constant;
import easyanimation.frame.Language;
import easyanimation.frame.MainWindow;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author tandn
 */
public class CRightPanel extends JPanel
{
    private final int COLOR_ROAD = 0x92b2dd;
    //private final int COLOR_ITEM = 0xf4a841;
    private final int COLOR_ITEM = 0xff00ff;
    //private final int COLOR_ITEM_SELECT = 0xfcee57;
    private final int COLOR_ITEM_SELECT = 0xf4a841;
    private final int MARGIN_TOP = 15;
    private final int MARGIN_LEFT = 8;
    private final int ITEM_VIEW_X = 16;
    private final int ITEM_VIEW_Y0 = 25;
    private final int ITEM_VIEW_W = Constant.WINDOW_WIDTH_RIGHT - (MARGIN_LEFT << 1) - ITEM_VIEW_X;
    private final int ITEM_VIEW_H = ITEM_VIEW_W;
    private final int ITEM_VIEW_W_SELECT = 4;
    private final int ITEM_VIEWS_BETWEEN = 8;
    //origin image 
    private Image m_image = null;
    private int m_indexCurrentI = 0;
    private int m_indexStartI = 0;
    private int m_realNumItemsI = 0;
    private int m_mouseXI = 0, m_mouseYI = 0;//mouse point
    private int m_mousePressedXI = 0, m_mousePressedYI = 0;//mouse point

    public CRightPanel()
    {
        setPreferredSize(new Dimension(Constant.WINDOW_WIDTH_RIGHT,
                Constant.WINDOW_HEIGHT - Constant.WINDOW_HEIGHT_HEADER - Constant.WINDOW_WIDTH_BOTTOM));
        setBorderTitle("");
        addMouseWheelListener(new MouseWheelListener()
        {
            @Override
            public void mouseWheelMoved(MouseWheelEvent e)
            {
                int iLengthItem = 0;
                switch (getLeftPanel().getSelectedIndex())
                {
                    case Constant.TAB_INDEX_FRAME:
                    {
                        iLengthItem = getLeftPanel().getTabModule().getTable().getRowCount();
                    }
                    break;
                    case Constant.TAB_INDEX_ANIMATION:
                    {
                        iLengthItem = getLeftPanel().getTabFrame().getTable().getRowCount();
                    }
                    break;
                }
                if (iLengthItem <= 0)
                {
                    return;
                }
                if (e.getWheelRotation() < 0) //up
                {
                    if (m_indexStartI > 0)
                    {
                        m_indexStartI--;
                    }
                }
                else
                {
                    if (iLengthItem > m_realNumItemsI && m_indexStartI < iLengthItem - m_realNumItemsI)
                    {
                        m_indexStartI++;
                    }
                }
                reupdateUI();
            }
        });
        addMouseListener(new MouseListener()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                m_mousePressedXI = e.getX();
                m_mousePressedYI = e.getY();
                int iLengthItem = getTotalItems();
                if (iLengthItem <= 0)
                {
                    return;
                }
                int indexChoosed = -1;
                for (int i = 0; i < iLengthItem; i++)
                {
                    if (m_mouseXI > ITEM_VIEW_X && m_mouseXI < ITEM_VIEW_X + ITEM_VIEW_W
                            && m_mouseYI > ITEM_VIEW_Y0 + i * (ITEM_VIEW_H + ITEM_VIEWS_BETWEEN)
                            && m_mouseYI < ITEM_VIEW_Y0 + (i + 1) * (ITEM_VIEW_H + ITEM_VIEWS_BETWEEN) - ITEM_VIEWS_BETWEEN)
                    {
                        indexChoosed = i;
                        break;
                    }
                }
                if (indexChoosed != -1)
                {
                    addToSubTable(indexChoosed + m_indexStartI);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e)
            {
            }

            @Override
            public void mouseExited(MouseEvent e)
            {
            }

            @Override
            public void mouseClicked(MouseEvent e)
            {
            }

            @Override
            public void mouseReleased(MouseEvent e)
            {
            }
        });
        addMouseMotionListener(new MouseMotionListener()
        {
            @Override
            public void mouseDragged(MouseEvent e)
            {
            }

            @Override
            public void mouseMoved(MouseEvent e)
            {
                m_mouseXI = e.getX();
                m_mouseYI = e.getY();
                int lengthItemI = getTotalItems();
                if (lengthItemI <= 0)
                {
                    return;
                }
                for (int i = 0; i < lengthItemI; i++)
                {
                    if (m_mouseXI > ITEM_VIEW_X && m_mouseXI < ITEM_VIEW_X + ITEM_VIEW_W
                            && m_mouseYI > ITEM_VIEW_Y0 + i * (ITEM_VIEW_H + ITEM_VIEWS_BETWEEN)
                            && m_mouseYI < ITEM_VIEW_Y0 + (i + 1) * (ITEM_VIEW_H + ITEM_VIEWS_BETWEEN) - ITEM_VIEWS_BETWEEN)
                    {
                        m_indexCurrentI = i;
                        break;
                    }
                }
                reupdateUI();
            }
        });
    }

    public int getTotalItems()
    {
        int totalItemsI = 0;
        switch (getLeftPanel().getSelectedIndex())
        {
            case Constant.TAB_INDEX_FRAME:
            {
                totalItemsI = getLeftPanel().getTabModule().getTable().getRowCount();
            }
            break;
            case Constant.TAB_INDEX_ANIMATION:
            {
                totalItemsI = getLeftPanel().getTabFrame().getTable().getRowCount();
            }
            break;
        }
        return totalItemsI;
    }

    public void addToSubTable(int indexChoosedI)
    {
        int[] itemInfoIs = new int[]
        {
            -1, //do not care
            indexChoosedI, //index
            0, //x
            0   //y
        };
        switch (getLeftPanel().getSelectedIndex())
        {
            case Constant.TAB_INDEX_FRAME:
            {
                int rowSubTableSelect = getLeftPanel().getSubTableModuleFrame().getTable().getSelectedRow();
                int rowSubTableCount = getLeftPanel().getSubTableModuleFrame().getTable().getRowCount();
                if (rowSubTableCount < 1
                        || rowSubTableSelect < 0
                        || rowSubTableSelect >= rowSubTableCount)
                {
                    if (getLeftPanel().getTabFrame().getTable().getRowCount() < 1)
                    {
                        JOptionPane.showMessageDialog(MainWindow.getInstance(), Language.MESSAGE_HAS_NO_FRAME[CConfig.LANGUAGE], Language.ERROR[CConfig.LANGUAGE], JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    else
                    {
                        getCenterPanel().updateFromTable(true, Constant.TABLE_ACTION_ADD_OUTSIDE, -1, itemInfoIs);
                    }
                }
                else
                {
                    getCenterPanel().updateFromTable(true,
                            Constant.TABLE_ACTION_UPDATE_INDEX,
                            getLeftPanel().getSubTableModuleFrame().getTable().getSelectedRow(),
                            itemInfoIs);
                }
            }
            break;
            case Constant.TAB_INDEX_ANIMATION:
            {
                if (getLeftPanel().getSubTableFrameAnimation().getTable().getRowCount() < 1
                        || getLeftPanel().getSubTableFrameAnimation().getTable().getSelectedRow() < 0)
                {
                    getCenterPanel().updateFromTable(true, Constant.TABLE_ACTION_ADD_OUTSIDE, -1, null);
                }
                else
                {
                    getCenterPanel().updateFromTable(true,
                            Constant.TABLE_ACTION_UPDATE_INDEX,
                            getLeftPanel().getSubTableFrameAnimation().getTable().getSelectedRow(),
                            itemInfoIs);
                }
            }
            break;
        }
        getLeftPanel().getSubTableModuleFrame().setAction(Constant.TABLE_ACTION_NONE);
    }

    public final void setBorderTitle(String strTitle)
    {
        setBorder(BorderFactory.createTitledBorder(strTitle));
    }

    @Override
    public void paint(Graphics g)
    {
        super.paint(g);
        g.setClip(MARGIN_LEFT, MARGIN_TOP, getWidth() - (MARGIN_LEFT << 1), getHeight() - (MARGIN_TOP << 1) + 7);
        g.setColor(new Color(COLOR_ROAD));
        g.fillRect(0, 0, getWidth(), getHeight());
        switch (getLeftPanel().getSelectedIndex())
        {
            case Constant.TAB_INDEX_FRAME:
            {
                paintModules(g, getLeftPanel().getTabModule().getTable().getRowCount());
            }
            break;
            case Constant.TAB_INDEX_ANIMATION:
            {
//                paintFrames(g, getLeftPanel().getTabFrame().getTable().getRowCount());
                paintModules(g, getLeftPanel().getTabModule().getTable().getRowCount());
            }
            break;
        }
    }

    private void paintFrames(Graphics g, int iLenghtItems)
    {
        int iNumVisibleItems = (getHeight() - MARGIN_TOP - ITEM_VIEW_Y0) / (ITEM_VIEW_H + ITEM_VIEWS_BETWEEN);
        m_realNumItemsI = ((iLenghtItems > iNumVisibleItems) ? iNumVisibleItems : iLenghtItems);
        for (int i = m_indexStartI; i < m_indexStartI + m_realNumItemsI; i++)
        {
            int iItemY = ITEM_VIEW_Y0 + (i - m_indexStartI) * (ITEM_VIEW_H + ITEM_VIEWS_BETWEEN);
            if ((i - m_indexStartI) == m_indexCurrentI)
            {
                g.setClip(ITEM_VIEW_X - ITEM_VIEW_W_SELECT,
                        iItemY - ITEM_VIEW_W_SELECT,
                        ITEM_VIEW_W + (ITEM_VIEW_W_SELECT << 1),
                        ITEM_VIEW_H + (ITEM_VIEW_W_SELECT << 1));
                g.setColor(new Color(COLOR_ITEM_SELECT));
                g.fillRect(0, 0, getWidth(), getHeight());
            }
            //draw background items
            //if ((i - m_iIndexStart) == iNumVisibleItems)
            //{
            //    g.setClip(ITEM_VIEW_X,
            //            ITEM_VIEW_Y0 + (i - m_iIndexStart) * (ITEM_VIEW_H + ITEM_VIEWS_BETWEEN),
            //            ITEM_VIEW_W,
            //            ITEM_VIEW_H + getHeight() - (ITEM_VIEW_Y0 + 7 + m_iRealNumItems * (ITEM_VIEW_H + ITEM_VIEWS_BETWEEN)));
            //}
            //else
            {
                g.setClip(ITEM_VIEW_X,
                        ITEM_VIEW_Y0 + (i - m_indexStartI) * (ITEM_VIEW_H + ITEM_VIEWS_BETWEEN),
                        ITEM_VIEW_W,
                        ITEM_VIEW_H);
            }
            g.setColor(new Color(COLOR_ITEM));
            g.fillRect(0, 0, getWidth(), getHeight());

            //get the image module info
            int[] frameInfoIs = new int[Constant.TOTAL_MODULE_INFOS];
            Object[] frameInfoObjects = getLeftPanel().getTabFrame().getRow(i);
            frameInfoIs[Constant.MODULE_INFO_X] = Integer.parseInt((String) frameInfoObjects[Constant.COLUMN_TAB_FRAME_X]);
            frameInfoIs[Constant.MODULE_INFO_Y] = Integer.parseInt((String) frameInfoObjects[Constant.COLUMN_TAB_FRAME_Y]);

            //draw image of items           
            int iDestinateX = ITEM_VIEW_X + (ITEM_VIEW_W - frameInfoIs[Constant.MODULE_INFO_W] >> 1);
            int iDestinateY = iItemY + (ITEM_VIEW_H - frameInfoIs[Constant.MODULE_INFO_H] >> 1);
            g.drawImage(m_image,
                    iDestinateX, //x coordinate of the first corner of the destination rectangle
                    iDestinateY, //y coordinate of the first corner of the destination rectangle
                    iDestinateX + frameInfoIs[Constant.MODULE_INFO_W], // the x coordinate of the second corner of the destination rectangle.
                    iDestinateY + frameInfoIs[Constant.MODULE_INFO_H], // the y coordinate of the second corner of the destination rectangle.
                    frameInfoIs[Constant.MODULE_INFO_X], //the x coordinate of the first corner of the source rectangle
                    frameInfoIs[Constant.MODULE_INFO_Y], //the y coordinate of the first corner of the source rectangle
                    frameInfoIs[Constant.MODULE_INFO_X] + frameInfoIs[Constant.MODULE_INFO_W], //the x coordinate of the second corner of the source rectangle.
                    frameInfoIs[Constant.MODULE_INFO_Y] + frameInfoIs[Constant.MODULE_INFO_H], //the y coordinate of the second corner of the source rectangle.
                    this);
        }
    }

    private void paintModules(Graphics g, int iLenghtItems)
    {
        int iNumVisibleItems = (getHeight() - MARGIN_TOP - ITEM_VIEW_Y0) / (ITEM_VIEW_H + ITEM_VIEWS_BETWEEN);
        m_realNumItemsI = ((iLenghtItems > iNumVisibleItems) ? iNumVisibleItems : iLenghtItems);
        for (int i = m_indexStartI; i < m_indexStartI + m_realNumItemsI; i++)
        {
            int iItemY = ITEM_VIEW_Y0 + (i - m_indexStartI) * (ITEM_VIEW_H + ITEM_VIEWS_BETWEEN);
            if ((i - m_indexStartI) == m_indexCurrentI)
            {
                g.setClip(ITEM_VIEW_X - ITEM_VIEW_W_SELECT,
                        iItemY - ITEM_VIEW_W_SELECT,
                        ITEM_VIEW_W + (ITEM_VIEW_W_SELECT << 1),
                        ITEM_VIEW_H + (ITEM_VIEW_W_SELECT << 1));
                g.setColor(new Color(COLOR_ITEM_SELECT));
                g.fillRect(0, 0, getWidth(), getHeight());
            }
            //draw background items
            //if ((i - m_iIndexStart) == iNumVisibleItems)
            //{
            //    g.setClip(ITEM_VIEW_X,
            //            ITEM_VIEW_Y0 + (i - m_iIndexStart) * (ITEM_VIEW_H + ITEM_VIEWS_BETWEEN),
            //            ITEM_VIEW_W,
            //            ITEM_VIEW_H + getHeight() - (ITEM_VIEW_Y0 + 7 + m_iRealNumItems * (ITEM_VIEW_H + ITEM_VIEWS_BETWEEN)));
            //}
            //else
            {
                g.setClip(ITEM_VIEW_X,
                        ITEM_VIEW_Y0 + (i - m_indexStartI) * (ITEM_VIEW_H + ITEM_VIEWS_BETWEEN),
                        ITEM_VIEW_W,
                        ITEM_VIEW_H);
            }
            g.setColor(new Color(COLOR_ITEM));
            g.fillRect(0, 0, getWidth(), getHeight());

            //get the image module info
            int[] moduleInfoIs = new int[Constant.TOTAL_MODULE_INFOS];
            Object[] moduleInfoObjects = getLeftPanel().getTabModule().getRow(i);
            for (int j = 0; j < Constant.TOTAL_MODULE_INFOS; j++)
            {
                moduleInfoIs[j] = Integer.parseInt((String) moduleInfoObjects[Constant.COLUMN_TAB_MODULE_X + j]);
            }

            //draw image of items           
            int iDestinateX = ITEM_VIEW_X + (ITEM_VIEW_W - moduleInfoIs[Constant.MODULE_INFO_W] >> 1);
            int iDestinateY = iItemY + (ITEM_VIEW_H - moduleInfoIs[Constant.MODULE_INFO_H] >> 1);
            g.drawImage(m_image,
                    iDestinateX, //x coordinate of the first corner of the destination rectangle
                    iDestinateY, //y coordinate of the first corner of the destination rectangle
                    iDestinateX + moduleInfoIs[Constant.MODULE_INFO_W], // the x coordinate of the second corner of the destination rectangle.
                    iDestinateY + moduleInfoIs[Constant.MODULE_INFO_H], // the y coordinate of the second corner of the destination rectangle.
                    moduleInfoIs[Constant.MODULE_INFO_X], //the x coordinate of the first corner of the source rectangle
                    moduleInfoIs[Constant.MODULE_INFO_Y], //the y coordinate of the first corner of the source rectangle
                    moduleInfoIs[Constant.MODULE_INFO_X] + moduleInfoIs[Constant.MODULE_INFO_W], //the x coordinate of the second corner of the source rectangle.
                    moduleInfoIs[Constant.MODULE_INFO_Y] + moduleInfoIs[Constant.MODULE_INFO_H], //the y coordinate of the second corner of the source rectangle.
                    this);
        }
    }

    public CLeftPanel getLeftPanel()
    {
        return MainWindow.getInstance().getLeftPanel();
    }

    public CCenterPanel getCenterPanel()
    {
        return MainWindow.getInstance().getCenterPanel();
    }

    public void setImage(Image image)
    {
        m_image = image;
    }

    public void reupdateUI()
    {
        updateUI();
    }

    /**
     * @param m_iIndexStart the m_iIndexStart to set
     */
    public void setIndexStart(int m_iIndexStart)
    {
        this.m_indexStartI = m_iIndexStart;
    }
}
